package config

import (
	"github.com/pelletier/go-toml"
	"log"
)

type SerialParam struct {
	Name string `toml:"name"`
	Baud int    `toml:"baud"`
}

type Mqtt struct {
	Enable   bool   `toml:"enable"`
	Broker   string `toml:"broker"`
	Port     int    `toml:"port"`
	Username string `toml:"username"`
	Password string `toml:"password"`
}

type Config struct {
	DeviceCode   string        `toml:"device_code"`
	SerialParams []SerialParam `toml:"serial"`
	Mqtt         `toml:"mqtt"`
}

var Conf Config

func init() {
	// 读取 TOML 配置文件
	configFile := "config.toml"
	configTree, err := toml.LoadFile(configFile)
	if err != nil {
		log.Fatalf("Error loading TOML file: %v", err)
	}

	// 将 TOML 配置映射到结构体
	err = configTree.Unmarshal(&Conf)
	if err != nil {
		log.Fatalf("Error unmarshaling TOML file: %v", err)
	}
}
